<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="异常状态" prop="exFlag">
        <el-select v-model="queryParams.exFlag" placeholder="请选择异常状态" clearable size="small">
          <el-option
            v-for="dict in exFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单信息" prop="attribute">
        <el-input
          v-model="queryParams.attribute"
          placeholder="请输入订单信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="tabActiveName"  @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane label="异常" name="1"></el-tab-pane>
      <el-tab-pane label="申诉" name="2"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="exceptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1" size="mini" type="danger">异常</el-tag>
          <el-tag v-else>申诉</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="异常状态" align="center" prop="exFlag" :formatter="exFlagFormat" width="150"/>
      <el-table-column label="异常说明" align="center" prop="exRemark" show-overflow-tooltip />
      <!-- <el-table-column label="异常图片" align="center" prop="exImg" /> -->
      <el-table-column label="订单号" align="center" prop="orderNo" width="180" />
      <el-table-column label="订单信息" align="center" prop="attribute" show-overflow-tooltip />
      <!-- <el-table-column label="区域" align="center" prop="areaCode" /> -->
      <el-table-column label="区域" align="center" prop="district" />
      <el-table-column label="地址" align="center" prop="address" width="200" />
      <el-table-column label="所属代理" align="center" prop="proxy" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="异常时间" align="center" prop="createTime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-guide" v-if="scope.row.status == 1"
            @click="handleDistr(scope.row)"
          >指派</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"  v-if="scope.row.status != 3"
            @click="handleDeal(scope.row)"
          >处理</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异常记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="订单号" prop="orderNo">
                <el-input v-model="form.orderNo" placeholder="请输入订单号" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="订单信息" prop="attribute">
                <el-input v-model="form.attribute" placeholder="请输入订单信息" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="异常状态" prop="exFlag">
                <el-select v-model="form.exFlag" placeholder="请选择异常状态" disabled>
                  <el-option
                    v-for="dict in exFlagOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="异常说明" prop="exRemark">
                <el-input v-model="form.exRemark" type="textarea" placeholder="请输入内容"  disabled/>
              </el-form-item>    
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
               <el-form-item label="区域" prop="district">
                <el-input v-model="form.district" placeholder="请输入区域"  disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入地址"  disabled/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"  />
              </el-form-item>
            </el-col>
           
          </el-row>
        
        <el-form-item label="选择代理" prop="proxyId">
          <el-select v-model="form.proxyId" placeholder="请选择区域代理" >
            <el-option
              v-for="dict in proxyOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
       
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定分派</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listException, getException, delException, dealException, addException, updateException, distributeException, exportException } from "@/api/order/exception";
import { treeListProxy } from "@/api/business/proxy";

export default {
  name: "Exception",
  components: {
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常记录表格数据
      exceptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 异常状态字典
      exFlagOptions: [],
      // 状态(1:已创建，2：已指派，3：已处理)字典
      statusOptions: [],
      proxyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exFlag: null,
        exRemark: null,
        exImg: null,
        orderId: null,
        orderNo: null,
        attribute: null,
        areaCode: null,
        district: null,
        address: null,
        proxyId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proxyId: [
          { required: true, message: "区域代理不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_order_ex_type").then(response => {
      this.exFlagOptions = response.data;
    });
    this.getDicts("tb_ex_order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getProxyList();
  },
  methods: {
    /** 查询异常记录列表 */
    getList() {
      this.loading = true;
      listException(this.queryParams).then(response => {
        this.exceptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getProxyList(){
      treeListProxy().then(response => {
        this.proxyOptions = response.data;
      });
    },
    // 异常状态字典翻译
    exFlagFormat(row, column) {
      return this.selectDictLabel(this.exFlagOptions, row.exFlag);
    },
    // 状态(1:已创建，2：已指派，3：已处理)字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleTabClick(tab){
      if (tab.name != 'all') {
        this.queryParams.type = tab.name;
      }else{
        this.queryParams.type = '';
      }
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        exFlag: null,
        createTime: null,
        exRemark: null,
        exImg: null,
        orderId: null,
        orderNo: null,
        attribute: null,
        areaCode: null,
        district: null,
        address: null,
        proxyId: null,
        status: "0",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加异常记录";
    },
    /** 修改按钮操作 */
    handleDistr(row) {
      this.reset();
      const id = row.id || this.ids
      getException(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "异常记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            distributeException(this.form).then(response => {
              this.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除异常记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delException(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 处理按钮操作 */
    handleDeal(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认已经处理该异常记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return dealException(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        })
    },
   
  }
};
</script>
